﻿using System;
using System.Collections.Generic;
using System.Linq;
using XFControls.Utils;
using XFControls.XFPanels.XFPanelItems;
using XFControls;
using System.Drawing;
using XFControls.XFPanels;

namespace XFSenseUI.XFItems
{
    public class SenseItemMoreItems : SenseItemBase, IXFItemContainer
    {
        public XFPanelBase RightPanel { get; set; }

        private int _height;
        private Rectangle _rec;

        public SenseItemMoreItems()
        {
            ItemType = XFItemType.Clickable;
            Style = new XFItemStyle
                    {
                        BorderBottomColor = Color.FromArgb(189, 182, 189),
                        DashStyleBottom = System.Drawing.Drawing2D.DashStyle.Dash,
                        TextColor = Color.Black,
                        TextFont = new Font(FontFamily.GenericSansSerif, 8, FontStyle.Regular),
                        SelectedTextFont = new Font(FontFamily.GenericSansSerif, 8, FontStyle.Regular),
                        SecondaryTextFont = new Font(FontFamily.GenericSansSerif, 7, FontStyle.Regular),
                        SelectedSecondaryTextFont = new Font(FontFamily.GenericSansSerif, 7, FontStyle.Regular),
                        SelectedBackgroundImageString = "item-select-background",
                        SecondaryTextColor = Color.FromArgb(57, 52, 57),
                        SelectedTextColor = Color.White,
                        SelectedBackgroundColor = Color.FromArgb(43, 36, 43),
                        SelectedSecondaryTextColor = Color.FromArgb(182, 178, 182),
                        Padding = 11,
                        PaddingBottom = 12,
                        PaddingLeft = 10,
                        PaddingRight = 16
                    };
        }

        public override XFItemClickResult GetClickResult()
        {
            if (RightPanel != null && !RightPanel.IsDisposed)
            {
                RightPanel.SavePanelAfterSlide = true;
                Parent.SetRightPanel(RightPanel, true);

                if (!RightPanel.HasBack && RightPanel is XFPanelList)
                    ((XFPanelList)RightPanel).Add(new XFItemBackHeader(), 1);

                if (Parent.Parent != null && Parent.Parent is XFPanelContainer)
                {
                    var cont = Parent.Parent as XFPanelContainer;
                    cont.SetHeaderLeftButton(new XFButtonBack());
                }

                return XFItemClickResult.SlideLeft;
            }

            return XFItemClickResult.Nothing;
        }

        public List<IXFItem> GetItems()
        {
            if(RightPanel == null || !(RightPanel is XFPanelList)) return null;

            var panel = RightPanel as XFPanelList;

            return panel.Cast<IXFItem>().ToList();
        }

        public override int GetHeight()
        {
            if (!Visible)
                return 0;

            if (_height > 0)
                return _height;

            _rec = this.GetImageRec("arrow-selected");
            _height = CalcRecs(_rec.Width == 0 ? 0 : _rec.Width + Style.PaddingLeft);

            if (Buffer == null)
                Buffer = XFBitmapFactory.GetBitmap(GetWidth(false), _height);

            return _height;
        }

        public override void ResetHeight()
        {
            _height = 0;
        }

        public override void ItemPaint(Graphics g, int x, int y)
        {
            base.ItemPaint(g, x, y);

            Font fontMain = getCurrentFont(false);
            Font fontSecondary = getCurrentFont(true);
            Color colorMain = getCurrentFontColor(false);
            Color colorSecondary = getCurrentFontColor(true);

            int width = _mRec.X + _mRec.Width + Style.PaddingLeft;

            if (!string.IsNullOrEmpty(MainText))
            {
                if (_mRec.IsEmpty)
                    CalcRecs(_rec.Width == 0 ? 0 : _rec.Width + Style.PaddingLeft);
                g.DrawString(MainText, _mRec, Style, fontMain, colorMain);
            }
            if (!string.IsNullOrEmpty(SecondaryText))
            {
                if (_sRec.IsEmpty)
                    CalcRecs(_rec.Width == 0 ? 0 : _rec.Width + Style.PaddingLeft); 
                g.DrawString(SecondaryText, _sRec, Style, fontSecondary, colorSecondary);
            }

            if (State == XFItemState.Selected)
            {
                if (_rec.Width != 0)
                {
                    int aHeight = (GetHeight() >> 1) - (_rec.Height >> 1);
                    DrawUI("arrow-selected", g, new Rectangle(width, aHeight, _rec.Width, _rec.Height));
                }
            }
            else
            {
                if (_rec.Width != 0)
                {
                    int aHeight = (GetHeight() >> 1) - (_rec.Height >> 1);
                    DrawUI("arrow", g, new Rectangle(width, aHeight, _rec.Width, _rec.Height));
                }
            }

            if (!string.IsNullOrEmpty(Icon))
                DrawUI(Icon, g, new Rectangle(Style.PaddingLeft, Style.PaddingTop, _iconSize, _iconSize));
        }
    }
}
